import { //核心
    React, Enhance, Controller, Component, $, EventEmitter,
    //
    tools, classnames,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Http,
    BoxWidget, Input, Button, Prompt, Link, Loaders, Editor, Resource, Directive, Lock, Fix
} from 'admin';
import {exp} from 'locutus/php/math';
import Brand from './w-brand'
import Cars from './w-cars'

/**
 * 车型
 */
class Carmodels extends Component {
    
    /**
     * 仓储对象缓存
     */
    static cache = null
    state = {
        edit: {},
        add: {}
    }
    
    _lang(name) {
        return `ysw.carmodels.${name}`;
    }
    
    /**
     * componentDidMount()在挂载结束之后马上被调用。需要DOM节点的初始化操作应该放在这里。
     */
    componentDidMount() {
        this.handlerLoadCache();
    }
    
    render() {
        let {edit, add} = this.state;
        return (
            <div>
                <Http ref="http"></Http>
                <Brand ref="brand" onCache={() => {
                    this.forceUpdate()
                }}></Brand>
                <Cars ref="cars" onCache={() => {
                    this.forceUpdate()
                }}></Cars>
                <Directive name="Modal">
                    <Dialog ref="addDialog" title={this._lang("add")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/carmodels/do_add/`}
                                onSuccess={this.handlerAddSuccess}
                                onError={this.handlerAddError}
                                rules={{
                                    "code": "required",
                                    "name": "required"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="brand_id" type="select" message={this._lang("brand")} option={Brand.cache}
                                   model={add} onValueChange={() => {
                                this.forceUpdate()
                            }}>
                              <div data-fix="buttons">
                                    <Button onClick={this._handlerAddBrandDialog}>
                                        <Icon icon="add"></Icon>
                                        <Lang lang="添加品牌"></Lang>
                                    </Button>
                                </div>
                            </Input>
                            <Input name="carscategory_id" type="select" message={this._lang("carscategory")}
                                   option={Cars.cache} group={add.brand_id} model={add}>
                                   <div data-fix="buttons">
                                    <Button onClick={this._handlerAddCarsDialog}>
                                        <Icon icon="add"></Icon>
                                        <Lang lang="添加车系"></Lang>
                                    </Button>
                                </div>
                                </Input>
                            <Input name="code" type="text" message={this._lang("code")}/>
                            <Input name="name" type="text" message={this._lang("name")}/>
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
                <Directive name="Modal">
                    <Dialog ref="editDialog" title={this._lang("edit")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/carmodels/do_edit/`}
                                onSuccess={this.handlerEditSuccess}
                                onError={this.handlerEditError}
                                rules={{
                                    "brand_id": "required",
                                    "carscategory_id": "required",
                                    "code": "required",
                                    "name": "required"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="id" type="hidden" model={edit}/>
                            <Input name="brand_id" type="select" message={this._lang("brand")} option={Brand.cache}
                                   model={edit}
                                   onValueChange={() => {
                                       this.forceUpdate()
                                   }}
                            >
                                <div data-fix="buttons">
                                    <Button onClick={this._handlerAddBrandDialog}>
                                        <Icon icon="add"></Icon>
                                        <Lang lang="添加品牌"></Lang>
                                    </Button>
                                </div>
                            </Input>
                            <Input name="carscategory_id" type="select" message={this._lang("carscategory")}
                                   ref='edit3' option={Cars.cache} model={edit} group={edit.brand_id || 0}
                            >
                                <div data-fix="buttons">
                                    <Button onClick={this._handlerAddCarsDialog}>
                                        <Icon icon="add"></Icon>
                                        <Lang lang="添加车系"></Lang>
                                    </Button>
                                </div>
                            </Input>
                            <Input name="code" type="text" message={this._lang("code")} model={edit}/>
                            <Input name="name" type="text" message={this._lang("name")} model={edit}/>
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
            </div>
        );
    }
    
    /**
     * 加载缓存
     */
    handlerLoadCache = () => {
        if (!Carmodels.cache) {
            this.refs.http.send(`${window.SERVER_URL}/ysw/carmodels/get_carmodels/`)
                .success((data) => {
                    Carmodels.cache = data;
                    this.props.onCache && this.props.onCache()
                })
                .error();
        }
    }
    
    handlerAddSuccess = (data) => {
        //
        this.props.onAddSuccess && this.props.onAddSuccess(data);
        //
        if (data) {
            Carmodels.cache = null;
            this.handlerLoadCache()
            this.hideAddDialog();
        }
    }
    
    handlerAddError = () => {
    
    }
    
    /**
     * 显示添加
     */
    showAddDialog() {
        this.refs.addDialog.show()
    }
    
    /**
     * 隐藏添加
     */
    hideAddDialog() {
        this.refs.addDialog.hide()
    }
    
    /**
     *
     * @param {*} edit
     */
    showEditDialog(edit) {
        this.setState({edit}, () => {
            this.refs.editDialog.show();
        });
    }
    
    hideEditDialog() {
        this.refs.editDialog.hide();
    }
    
    /**
     *
     */
    handlerEditSuccess = (data) => {
        //
        this.props.onEditSuccess && this.props.onEditSuccess(data);
        //
        if (data) {
            Carmodels.cache = null;
            this.handlerLoadCache();
            this.hideEditDialog();
            
        }
    }
    
    handlerEditError = () => {
        this.props.onEditError && this.props.onEditError();
    }
    
    _handlerAddBrandDialog() {
        this.refs.brand.showAddDialog()
    }
    
    /**
     *
     * @private
     */
    _handlerAddCarsDialog() {
        this.refs.cars.showAddDialog()
    }
}

Enhance(Carmodels)
export default Carmodels
